package itsm.isperp.job;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.workflow.service.BpmProcessRunService;

import java.util.List;
import java.util.Map;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * 
 * 
 * 
 * @author lizx
 * @date 2015-6-30
 * @since 1.0
 * 
 */
@Service
public class AutoSatisfactionJob implements Job {

	public static final Logger logger = LoggerFactory
			.getLogger(AutoSatisfactionJob.class);

	@Override
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		JdbcTemplate jdbcTemplate = ContextHolder.getSpringBean("jdbcTemplate");
		BpmProcessRunService bpmProcessRunService = ContextHolder
				.getSpringBean("bpmProcessRunService");
		try {
			//获取24小时内没有评分且来源是Web的
			String sql="select * from icd_incident where source='Web' and satisfaction='0' and solved_date is not null and TIMESTAMPDIFF(HOUR,solved_date,now())>24";
			
			List<Map<String,Object>> list=jdbcTemplate.queryForList(sql);
			
			for(Map<String,Object> li:list)
			{
				String id=li.get("id").toString();
				String processId=li.get("process_id").toString();
				String sql2="update icd_incident set satisfaction='4',customer_opinion='系统评分' where id='"+id+"'";
				jdbcTemplate.update(sql2);
				bpmProcessRunService.endProcess("admin",
						processId, "系统评分");
			}
			logger.info("系统自动评分完成！");
			
		} catch (Exception e) {
			logger.error("自动评分失败，原因是：" + e.getMessage(), e);
		}


	}

	
}
